﻿package{
	import flash.display.Sprite;
	import flash.events.*;
	import flash.geom.Point;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.xml.*;
	public class LearnSprite extends Sprite{
		private var learnButton:myButton;
		private var lSprite:Sprite;
		private var fillSprite:Sprite;
		private var mainSprite:Sprite;
		private var cc:ChineseCharacter;
		private var strokeShapes:Array;
		private var pointsHandle:Array;
		private var strokes:Array;
		private var spd:int;
		private var m:int;
		private var fillColor:uint;
		private var bm:Bitmap;
		private var bmdata:BitmapData;
		private var flag:Boolean;
		private var style:XMLList;
		private var cfWidth:Number;
		public function LearnSprite(c:ChineseCharacter,stl:XMLList){
				this.style=stl;
				this.cfWidth=Number(this.style.characterFrame.width);
				this.mainSprite=new Sprite();
				this.fillSprite=new Sprite();
				this.spd=style.learnSprite.fillSprite.fillSpeed;
				this.m=0;
				this.flag=false;
				this.bmdata=new BitmapData(350,350,true,0x00000000);
				this.bm=new Bitmap(bmdata);
				this.bm.scaleX=style.learnSprite.fillSprite.strokeShapeScale;
				this.bm.scaleY=style.learnSprite.fillSprite.strokeShapeScale;
				this.bm.x=0;
				this.bm.y=0;
				
				this.fillColor=style.learnSprite.fillSprite.fillColor;
				//this.fillColor=0xf0000000;
				this.fillSprite.x=style.learnSprite.fillSprite.x;
				this.fillSprite.y=style.learnSprite.fillSprite.y;
				this.fillSprite.scaleX=1;
				this.fillSprite.scaleY=1;
				this.pointsHandle=new Array();
				this.cc=c;
				this.strokeShapes=cc.getStrokeShapeForFill();
				this.strokes=cc.getStrokes();
				this.mainSprite.x=0;
				this.mainSprite.useHandCursor=true;
				this.lSprite=new Sprite();
				this.lSprite.x=0;
				this.lSprite.y=0;
				this.lSprite.graphics.lineStyle(1,0x000000);
				this.lSprite.graphics.beginFill(style.learnSprite.background_color);
				this.lSprite.graphics.drawRoundRect(0,0,style.learnSprite.width,style.learnSprite.height,25,25);
				this.lSprite.graphics.endFill();
				this.lSprite.scaleX=1;
				this.lSprite.scaleY=(Number(style.characterFrame.learnButton.height)+2)/style.learnSprite.height;
				this.lSprite.addEventListener(MouseEvent.CLICK,learnSpriteHidde);
				this.fillSprite.addEventListener(MouseEvent.CLICK,fillSpriteClickShow);
				this.mainSprite.addChild(this.lSprite);
				for(var k=0;k<this.strokeShapes.length;k++){
					
					for(var j=0;j<this.strokeShapes[k].length;j++){
						this.strokeShapes[k][j].x=0;
						this.strokeShapes[k][j].y=0;
						this.strokeShapes[k][j].scaleX=style.learnSprite.fillSprite.strokeShapeScale;
						this.strokeShapes[k][j].scaleY=style.learnSprite.fillSprite.strokeShapeScale;
						this.fillSprite.addChild(this.strokeShapes[k][j]);
						}
					}
				
				this.fillSprite.visible=false;
				this.mainSprite.addChild(fillSprite);
				
				this.fillSprite.addChild(bm);
				
				//this.fillSprite.setChildIndex(bm,2);
				this.mainSprite.setChildIndex(fillSprite,1);
				learnButton=new myButton("Learn",Number(style.characterFrame.learnButton.width),Number(style.characterFrame.learnButton.height));
				learnButton.x=1;
				learnButton.y=1;
				learnButton.scaleX=1;
				learnButton.scaleY=1;
				
				this.learnButton.addEventListener(MouseEvent.CLICK,learnSpriteShow);
				this.mainSprite.addChild(learnButton);
				this.mainSprite.setChildIndex(learnButton,2);
				this.mainSprite.setChildIndex(lSprite,0);
				this.addChild(this.mainSprite);
				//this.addEventListener(Event.ENTER_FRAME,setPointsHandle);
				setPointsHandle();
			
			}
		private function initFillSprite(){
				this.fillSprite.removeChild(bm);
				this.bm=null;
				this.bmdata=new BitmapData(350,350,true,0x00000000);
				this.bm=new Bitmap(bmdata);
				this.bm.scaleX=style.learnSprite.fillSprite.strokeShapeScale;
				this.bm.scaleY=style.learnSprite.fillSprite.strokeShapeScale;
				this.fillSprite.addChild(bm);
				
				
			}
			
		private function setPointsHandle(){
			
				for(var kk:int=0;kk<this.strokes.length;kk++){
					var currentShape= this.strokeShapes[kk];//
					var dd:FillDirection=new FillDirection();		
					
					var currentFillDirection=dd.getFillDirection(strokes[kk]);//获取填充方向。不同的填充方向代表的不同的填充方式：0-横向正， 2-横向负，1-纵向正，3-纵
					//trace(currentFillDirection.join("")+"/"+currentShape.length);
					if(currentFillDirection.length!=currentShape.length){//如果分段数和填充方向数不同则报错
							trace(kk);
							throw new Error('fillDirection and shapes have the different length');
					}
					  
					for(var ii:int=0;ii<currentFillDirection.length;ii++){
					
						if(currentFillDirection[ii]!=0&&currentFillDirection[ii]!=1&&currentFillDirection[ii]!=2&&currentFillDirection[ii]!=3){//检查数据可用性
							throw new Error('fillDirection has the error data.');
						}
						
						if(currentFillDirection[ii]=="0"||currentFillDirection[ii]=="1")//如果是横向的正或者纵向的正，则按照递增方式调用store
						{
							for(var j:int=0;j<=350;j++)
							{
								for( var i:int=0;i<=350;i++)
								{
								
								store(currentFillDirection[ii],currentShape[ii],i,j);
								
								
								}
							}
						}
					
						else   if(currentFillDirection[ii]=="2"||currentFillDirection[ii]=="3")//如果是反向的横或者反向的竖，则按照递减方式调用store
						{
							for( j=350;j>=0;j--)
							{
								for( i=350;i>=0;i--)
								{
								store(currentFillDirection[ii],currentShape[ii],i,j);
								
								}
							}
						}
					
				}
				
					
				
				}
				this.removeEventListener(Event.ENTER_FRAME,setPointsHandle);
				this.flag=true;
			}
		private function store(currentFillDirection:String,currentShape:Bitmap,i:int,j:int):void
			{
					//trace(this.currentShape[k]);
					var aColor:uint=0;//
					if(currentFillDirection=="0"||currentFillDirection=="2")//总的来说。，这里的扫描方式是，正向横：递增式列优先扫描，反向横：递减式列优先扫描，正向竖：递增式行优先扫描，反向竖：递减式行优先扫描
					{
						aColor=currentShape.bitmapData.getPixel32(j,i);//读取点的颜色
						
					}
						
					else
					{
						aColor=currentShape.bitmapData.getPixel32(i,j);
					}
					
					var alpha0:uint=aColor>>24;//获取颜色值（32）的前8位，若为黑色，则为ff，无色则为0
				//	var color:uint=aColor<<8;
					if(alpha0>0)
					{
						
						//trace(alpha0);
						
						var point:Point=new Point();
						if(currentFillDirection=="0"||currentFillDirection=="2")
						{
							
							point.x=j;
							point.y=i;
							//this.currentShape[k].bitmapData.setPixel32(j,i,0xffff0000);//设置颜色
						}
						else
						{
							point.x=i;
							point.y=j;
							//this.currentShape[k].bitmapData.setPixel32(i,j,0xff000000);
						}
						//strokeArray.push(points);
						pointsHandle.push(point);
					//	strokes[k].bmp.bitmapData.setPixel32(i,j,0xffff00ff);
					}
			
			}
			
		function learnSpriteShow(e:MouseEvent){
				e.stopPropagation();
				if(!this.hasEventListener(Event.ENTER_FRAME)){
					
					this.addEventListener(Event.ENTER_FRAME,fillSpriteShow);
				}
				else{
					this.removeEventListener(Event.ENTER_FRAME,fillSpriteHidde);
					this.addEventListener(Event.ENTER_FRAME,fillSpriteShow);
					}
				this.learnButton.disableStyleChange();
				this.learnButton.removeEventListener(MouseEvent.CLICK,learnSpriteShow);
				this.learnButton.addEventListener(MouseEvent.CLICK,learnSpriteHidde);
			}
		function fillSpriteClickShow(e:MouseEvent){
			e.stopPropagation();
			
					
					this.fillSprite.visible=true;
					this.removeEventListener(Event.ENTER_FRAME,fillSpriteShow);
					trace(this.fillSprite.hasEventListener(Event.ENTER_FRAME));
					if(!this.fillSprite.hasEventListener(Event.ENTER_FRAME)){
						this.fillSprite.addEventListener(Event.ENTER_FRAME,fill);
						this.m=0;
						}
					initFillSprite();
					this.m=0;
					
			}
		function fillSpriteShow(e:Event){
			
				
				if(this.mainSprite.x>Number(style.learnSprite.move_left)){
					this.mainSprite.x-=Number(style.learnSprite.move_left_speed);
					
					}
				else if(this.lSprite.scaleY<1){
					this.lSprite.scaleY+=Number(style.learnSprite.move_down_speed);
					}
				else{
					
					this.fillSprite.visible=true;
					this.removeEventListener(Event.ENTER_FRAME,fillSpriteShow);
					if(!this.fillSprite.hasEventListener(Event.ENTER_FRAME)){
						this.fillSprite.addEventListener(Event.ENTER_FRAME,fill);
						this.m=0;
						}
					initFillSprite();
					this.m=0;
					}
				
			}
		function fill(e:Event){
			if(this.flag==true){
				for(var i=0;i<this.spd;i++)//每一帧中填充spd个点
					{
					m=m+1;
				
					if(m<pointsHandle.length)
						{   
						
						this.bm.bitmapData.setPixel32(pointsHandle[m].x,pointsHandle[m].y,this.fillColor);//依次设置颜色
						
						
						}
					else if(m>pointsHandle.length)
					{
						//initFillSprite();
					//this.fillSprite.removeEventListener(Event.ENTER_FRAME,fill);
					//this.m=0;
					
						}
					}
					//this.bm.scaleX=style.learnSprite.fillSprite.strokeShapeScale;
					//this.bm.scaleY=style.learnSprite.fillSprite.strokeShapeScale;
					//this.bm.x=0;
					//this.bm.y=0;
			}
			}
		public function letlearnSpriteHide(){
			if(this.fillSprite.hasEventListener(Event.ENTER_FRAME)){
						this.fillSprite.removeEventListener(Event.ENTER_FRAME,fill);
						
						}
			
			
			if(!this.hasEventListener(Event.ENTER_FRAME)){
					
					this.addEventListener(Event.ENTER_FRAME,fillSpriteHidde);
				}
				else{
					
					this.removeEventListener(Event.ENTER_FRAME,fillSpriteShow);
					this.addEventListener(Event.ENTER_FRAME,fillSpriteHidde);
					}
				initFillSprite();
			
			this.learnButton.removeEventListener(MouseEvent.CLICK,learnSpriteHidde);
				this.learnButton.addEventListener(MouseEvent.CLICK,learnSpriteShow);
			}
		function learnSpriteHidde(e:MouseEvent){
			e.stopPropagation();
			letlearnSpriteHide();
			}
		function fillSpriteHidde(e:Event){
				this.fillSprite.visible=false;
				if(this.lSprite.scaleY>(Number(style.characterFrame.learnButton.height)+2)/style.learnSprite.height){
					this.lSprite.scaleY-=Number(style.learnSprite.move_down_speed);
					}
					
				else if(this.mainSprite.x<0){
					this.mainSprite.x+=Number(style.learnSprite.move_left_speed);
					
					}
				else{
					this.removeEventListener(Event.ENTER_FRAME,fillSpriteHidde);
					this.learnButton.enableStyleChange();
					this.lSprite.scaleY=(Number(style.characterFrame.learnButton.height)+2)/style.learnSprite.height;
					}
				
				
			
			}
		}
	}